package com.example.speech_recognition.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.example.speech_recognition.R;
import com.example.speech_recognition.bean.DictationResult;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechRecognizer;
import com.iflytek.cloud.ui.RecognizerDialog;
import com.iflytek.cloud.ui.RecognizerDialogListener;

import java.util.ArrayList;
import java.util.List;

public class SpeechActivity extends AppCompatActivity implements View.OnClickListener {
    private SpeechRecognizer mSpeechRecognizer;
    private RecognizerDialog dialog;

    private Button btn_speech;
    private ListView lv_speech;

    private List<DictationResult> beans;
    private List<String> lists;

    private ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_speech);
        initView();
        beans = new ArrayList<DictationResult>();
        lists = new ArrayList<String>();

        adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,lists);
        lv_speech.setAdapter(adapter);

        dialog = new RecognizerDialog(SpeechActivity.this, mInitListener);

    }

    /**
     * 点击按钮之后 显示RecognizerDialog
     */

    private void showDialog() {
        dialog.setParameter(SpeechConstant.DOMAIN, "iat");
        dialog.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
        dialog.setParameter(SpeechConstant.ACCENT, "mandarin");
        dialog.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);
        //若要将UI控件用于语义理解，必须添加以下参数设置，设置之后onResult回调返回将是语义理解结果
        // mDialog.setParameter("asr_sch", "1");
        // mDialog.setParameter("nlp_version", "2.0");
        dialog.setListener(new RecognizerDialogListener() {
            String resultJson = "[";//放置在外边做类的变量则报错，会造成json格式不对

            @Override
            public void onResult(RecognizerResult recognizerResult, boolean b) {

                Log.e("recognizerResult", recognizerResult.getResultString());
                if (!b) {
                    resultJson += recognizerResult.getResultString() + ",";
                } else {
                    resultJson += recognizerResult.getResultString() + "]";
                }

                if (b) {
                    Log.e("resultJson", resultJson);
                    //解析语音识别后返回的json格式的结果
                    Gson gson = new Gson();
                    String result = "";
                    beans = gson.fromJson(resultJson,
                            new TypeToken<List<DictationResult>>() {}.getType());
                    for (int i = 0; i < beans.size(); i++) {
                        result += beans.get(i).toString();
                    }
                    Log.e("result", result);
                    lists.add(result);
                    adapter.notifyDataSetChanged();
                }

            }

            @Override
            public void onError(SpeechError speechError) {

            }
        });
        dialog.show();
    }


    /**
     * 初始化监听器
     */
    private InitListener mInitListener = new InitListener() {
        @Override
        public void onInit(int i) {
            if (i != ErrorCode.SUCCESS) {
                Toast.makeText(SpeechActivity.this, "初始化失败，错误码：" + i, Toast.LENGTH_SHORT).show();
            }
        }
    };

    private void initView() {
        btn_speech = (Button) findViewById(R.id.btn_speech);
        lv_speech = (ListView) findViewById(R.id.lv_speech);

        btn_speech.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_speech:
                showDialog();
                break;
        }
    }
}
